using System;
using System.Collections.Generic;
using System.Linq;
using Pay.DTO;
using Pay.IService;
using Pay.Data;
using System.Linq.Expressions;
using Pay.Service.Cache;
using Pay.Core;
using Pay.TO;

namespace Pay.Service {
    public class MessagePushService : ServiceBase, IMessagePushService, IDisposable {
        private IUnitOfWork _iUnitOfWork;

        public MessagePushService(IUnitOfWork iUnitOfWork) : base(iUnitOfWork) {
            _iUnitOfWork = iUnitOfWork;
        }
        /// <summary>
        /// 系统消息
        /// </summary>
        /// <returns>The get.</returns>
        /// <param name="query">Query.</param>
        public PagedResult<message_push_dto> Get(MessageQuery query) {
            Expression<Func<MessagePush, bool>> filter = c => c.MerchantId == query.user_id;

            if (query.msg_type > 0) {
                if (query.msg_type == 1)
                    filter = filter.And(c => c.Type == MsgType.None || (c.Type >= MsgType.BazaarPass && c.Type < MsgType.TransferProftFail));
                else if (query.msg_type == 2)
                    filter = filter.And(c => c.Type >= MsgType.TransferProftFail && c.Type < MsgType.UserFeedBack);
                else if (query.msg_type == 3)
                    filter = filter.And(c => c.Type >= MsgType.UserFeedBack && c.Type < MsgType.ServiceTo);
                else if (query.msg_type == 4)
                    filter = filter.And(c => c.Type >= MsgType.OrderProft);
            } else {
                filter = filter.And(c => c.Type == (MsgType)query.type);
            }

            return context.MessagePushs.FromCachePaged<MessagePush, message_push_dto>(query.page, query.size, filter, orderBy => orderBy.OrderByDescending(c => c.CreateTime), "MessagePush" + query.user_id);
        }

        public message_push_dto Get(long id) {
            var model = context.MessagePushs.FindById(id);
            if (!model.IsRead) {
                model.IsRead = true;
                _iUnitOfWork.Commit("MessagePush" + model.MerchantId);
            }
            return model.ToDtoObject<message_push_dto>();
        }

        public int Insert(message_push_dto dto) {
            var model = dto.ToDtoObject<MessagePush>();
            context.MessagePushs.Add(model);
            int result = _iUnitOfWork.Commit("MessagePush" + dto.user_id);
            return result;
        }

        public int Update(message_push_dto dto) {
            var model = context.MessagePushs.FindById(dto.id);
            dto.ToDtoObject(model);
            int result = _iUnitOfWork.Commit("MessagePush" + dto.user_id);
            return result;
        }

        public int Delete(List<long> ids) {
            if (ids.Count == 0) return 0;
            Dictionary<long, string> tags = new Dictionary<long, string>();
            foreach (var id in ids) {
                var model = context.MessagePushs.FindById(id);
                context.MessagePushs.Remove(model);
                if (!tags.ContainsKey(model.MerchantId))
                    tags.Add(model.MerchantId, "MessagePush" + model.MerchantId);
            }
            var tag = tags.Values.ToArray();
            int result = _iUnitOfWork.Commit(tag);
            return result;
        }
    }
}